package com.aiti.lulian.controller.lulian;


import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.base.core.utils.obj.BeanCopyUtil;
import com.aiti.lulian.dto.LuLianPurchaseDemandQuoteDto;
import com.aiti.lulian.entity.LuLianEnterprise;
import com.aiti.lulian.entity.LuLianEnterpriseUser;
import com.aiti.lulian.entity.LuLianPurchaseDemandQuote;
import com.aiti.lulian.service.ILuLianEnterpriseService;
import com.aiti.lulian.service.ILuLianEnterpriseUserService;
import com.aiti.lulian.service.ILuLianPurchaseDemandQuoteService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("LuLianPurchaseDemand")
@Api(tags = " LuLianPurchaseDemand", description = "采购需求")
public class LuLianPurchaseDemandQuoteController extends WebController {
    protected final Logger logger = LoggerFactory.getLogger(LuLianPurchaseDemandQuoteController.class);

    @Autowired
    private ILuLianEnterpriseUserService enterpriseUserService;

    @Autowired
    private ILuLianEnterpriseService luLianEnterpriseService;

    @Autowired
    private ILuLianPurchaseDemandQuoteService purchaseDemandQuoteService;

    //采购需求报价
    @ApiOperation(value = "采购需求报价")
    @PostMapping("addQuote")
    public Message save(@RequestBody LuLianPurchaseDemandQuoteDto luLianPurchaseDemandQuoteDto) {
        try {
            if (luLianPurchaseDemandQuoteDto == null) {
                logger.error("新增异常：===》入参异常");
                return Message.fail("入参异常");
            }
            //检查当前是否为已登录状态
            String userId = findUserId();
            if (StrUtil.isBlank(userId)) {
                System.out.println("------------------未登录------------------");
                return Fail("请先注册/登录！");
            }
            //检查是否为已认证企业的账号
            LambdaQueryWrapper<LuLianEnterpriseUser> enterpriseUserWrapper = new LambdaQueryWrapper<>();
            enterpriseUserWrapper.eq(LuLianEnterpriseUser::getUserId, userId);
            List<LuLianEnterpriseUser> enterpriseUserList = enterpriseUserService.list(enterpriseUserWrapper);
            List<String> enterpriseIds = enterpriseUserList.stream().map(LuLianEnterpriseUser::getEnterpriseId).collect(Collectors.toList());
            if (enterpriseIds == null || enterpriseIds.size() == 0) {
                enterpriseIds.add("0");
            }
            LambdaQueryWrapper<LuLianEnterprise> enterpriseHistoryWrapper = new LambdaQueryWrapper<>();
            enterpriseHistoryWrapper.in(LuLianEnterprise::getBaseId, enterpriseIds);
            int count = luLianEnterpriseService.count(enterpriseHistoryWrapper);
            if (count == 0) {
                return Fail("该账号未认证企业，请先进行企业认证！");
            } else if (count == 1 && enterpriseUserList.size() > 1) {
                return Fail("该账号的企业关联记录存在多条！");
            } else if (count > 1) {
                return Fail("该账号关联了多个认证企业！");
            }
            luLianPurchaseDemandQuoteDto.setCreator(userId);
            LuLianPurchaseDemandQuote luLianPurchaseDemandQuote = BeanCopyUtil.convertBean(luLianPurchaseDemandQuoteDto, LuLianPurchaseDemandQuote.class);
            return Success(purchaseDemandQuoteService.save(luLianPurchaseDemandQuote));
        } catch (Exception e) {
            logger.error("新增异常：===》" + e);
            return Message.fail(e.getMessage());
        }
    }

}


